<template>
  <div>
    <van-field label='标题' v-model='form.title'/>
    <van-field label='图片' v-model='form.pic'/>
    <van-field label='描述' v-model='form.descrip'/>
    选择讲师
    <select v-model='form.teacher_id'>
      <option :value="i.id" v-for='i in tlist'>{{ i.name }}</option>
    </select>
    选择分类
    <reply :data='cates'/>
    动态参数
    <div v-if='params'>
      <van-field v-for="(v,k) in params" :label='v' v-model="values[v]"/>
    </div>


    <van-field name="radio" label="状态">
      <template #input>
        <van-radio-group v-model="form.status" direction="horizontal">
          <van-radio name="1">免费</van-radio>
          <van-radio name="2">收费</van-radio>
        </van-radio-group>
      </template>
    </van-field>

    <van-button @click='add'>添加</van-button>
  </div>
</template>
<script>
import bus from './bus.js'
import reply from "../reply";

export default {
  components: {
    'reply': reply
  },
  data() {
    return {
      tlist: [{'id': 1, 'name': '王老师'}, {'id': 2, 'name': '李老师'}],
      cates: {},
      params: {},
      values: {},
      form: {pid: 0, params: '', status: '2'}
    }
  },
  methods: {
    //获取分类列表
    getlist() {
      this.axios.get('cateM').then(res => {
        this.cates = {'id': 0, 'name': 'sdfdsf', 'son': res.data.list}
      })
    },
    add() {w
      if (this.values) {
        this.form.params = JSON.stringify(this.values)
      }
      this.axios.post('courseM', this.form).then(res => {

      })
    }
  },
  mounted() {
    this.getlist()
    bus.$on('pid', val => {
      this.form.pid = val
      //通过pid查询分类params
      this.axios.get('getCateByid?id=' + val).then(res => {
        if (res.data.params) {
          this.params = JSON.parse(res.data.params)
        } else {
          this.params = {}
        }

      })
    })
  },
}
</script>
<style lang="">

</style>
